package com.zhuiyun.educations.api.newCourse.mapper;

import com.zhuiyun.educations.api.newCourse.entity.Course;
import com.zhuiyun.educations.api.newCourse.entity.DTO.CourseDTO;
import com.zhuiyun.educations.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName ICourseMapper
 * @Description TODO 课程后端持久层
 * @Author 杨友朋
 * @Date 2023/6/28 16:54
 **/
@Mapper
public interface ICourseMapper extends BaseMapper<Course> {
    /**
     * @Author 杨友朋
     * @Description TODO 全查课程信息（后台管理）
     * @Date 2023/6/28 16:41
     * @Return List<CourseInfo>
     * @param courseDTO
     */
    @Select("<script>SELECT i.*, s.*, t.*, c.*, p.* FROM course_info i LEFT JOIN course c ON c.id = i.course_id LEFT JOIN subject s ON s.id = c.subject_id LEFT JOIN teacher t ON t.id = i.teacher_id LEFT JOIN price p ON p.course_id = i.id WHERE i.del_flag = 0" +
            "<if test='subjectId!=null and subjectId!=&quot;&quot;'> and subject_id=#{subjectId}</if>" +
            "<if test='teacherId!=null and teacherId!=&quot;&quot;'> and teacher_id=#{teacherId}</if>" +
            "<if test='payType!=null and payType!=&quot;&quot;'> and pay_type=#{payType}</if>" +
            "<if test='courseTitle!=null and courseTitle!=&quot;&quot;'> and course_title like concat('%',#{courseTitle},'%')</if>" +
            "<if test='courseStatus!=null and courseStatus!=&quot;&quot;'> and course_status=#{courseStatus}</if>" +
            "</script>")
    List<CourseDTO> selectCourse(CourseDTO courseDTO);

    /**
     * @Author 杨友朋
     * @Description TODO 全查课程信息（前端pc端）
     * @Date 2023/6/28 16:41
     * @Return List<CourseInfo>
     * @param courseDTO
     */
    @Select("<script>SELECT i.*, s.*, t.*, c.*, p.* FROM course_info i " +
            "LEFT JOIN course c ON c.id = i.course_id LEFT JOIN subject s ON s.id = c.subject_id LEFT JOIN teacher t ON t.id = i.teacher_id LEFT JOIN price p ON p.course_id = i.id " +
            "WHERE i.del_flag = 0 and course_status='0'" +
            "<if test='subjectId!=null and subjectId!=&quot;&quot;'> and subject_id=#{subjectId}</if>" +
            "<if test='teacherId!=null and teacherId!=&quot;&quot;'> and teacher_id=#{teacherId}</if>" +
            "<if test='payType!=null and payType!=&quot;&quot;'> and pay_type=#{payType}</if>" +
            "<if test='courseTitle!=null and courseTitle!=&quot;&quot;'> and course_title like concat('%',#{courseTitle},'%')</if>" +
            "<if test='courseStatus!=null and courseStatus!=&quot;&quot;'> and course_status=#{courseStatus}</if>" +
            "</script>")
    List<CourseDTO> selectCoursePc(CourseDTO courseDTO);

    /**
     * @Author 杨友朋
     * @Description TODO 根据课程查章节
     * @Date 2023年6月28日19:04:01
     * @Return List<CourseInfo>
     * @param courseDTO
     */
    @Select("select u.* from course_info i,course_unit u where u.del_flag=0 and u.course_id=i.id and i.id=#{courseId}")
    List<CourseDTO> selectUnit(CourseDTO courseDTO);

    /**
     * @Author 杨友朋
     * @Description TODO 根据章节查详细课程
     * @Date 2023年6月28日19:55:23
     * @Return List<CourseInfo>
     * @param courseDTO
     */
    @Select("SELECT j.*,f.file_path FROM unit_joint j " +
            "LEFT JOIN course_unit u ON j.unit_id = u.id LEFT JOIN file_merge f ON j.video_id = f.id " +
            "WHERE j.del_flag = 0 and j.unit_id=#{unitId}")
    List<CourseDTO> selectJoint(CourseDTO courseDTO);

    /**
    * @Author 杨友朋
    * @Description TODO 添加课程
    * @Date 2023/6/29 10:05
    * @Return Integer
    * @param course
    */
    @Insert("insert into course (course_name,subject_id,create_time,update_time) values (#{courseName},#{subjectId},#{createTime},#{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertCourse(Course course);

    /**
    * @Author 杨友朋
    * @Description TODO 根据id查询课程
    * @Date 2023/6/29 10:06
    * @Return Course
    * @param id
    */
    @Select("select * from course where id=#{id}")
    Course selectOneById(Integer id);

}
